package com.wpw.dream.excel.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.wpw.dream.util.date.DateUtils;


public class ExportExcelUtils {

    private static Log log = LogFactory.getLog(ExportExcelUtils.class);

    public static void write(HttpServletResponse response, HSSFSheet sheet) {
        try {

            // Retrieve the output stream
            ServletOutputStream outputStream = response.getOutputStream();
            // Write to the output stream
            sheet.getWorkbook().write(outputStream);
            // 清除缓存
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            log.error("ExportExcelUtils.write error:", e);
        }
    }

    public static <T> Object getFieldValue(T object, Field field) {
        Object val = null;
        try {
            Method m = (Method) object.getClass().getMethod(
                    "get" + getMethodName(field.getName()));
            if (null != object && null != m.invoke(object)) {
                if (field.getType() == String.class) {
                    val = (String) m.invoke(object);// 调用getter方法获取属性值
                } else if (field.getType() == int.class
                        || field.getType() == Integer.class) {
                    val = (Integer) m.invoke(object);
                } else if (field.getType() == long.class
                        || field.getType() == Long.class) {
                    val = (Long) m.invoke(object);
                } else if (field.getType() == double.class
                        || field.getType() == Double.class) {
                    val = (Double) m.invoke(object);
                } else if (field.getType() == boolean.class
                        || field.getType() == Boolean.class) {
                    val = (Boolean) m.invoke(object);
                } else if (field.getType() == Date.class) {
                	Export export = field.getAnnotation(Export.class);
                	String dateFormat = export.dateFormat();
                //	DateTime
                    val = DateUtils.dateToString((Date) m.invoke(object), dateFormat);
                } else if (field.getType() == short.class
                        || field.getType() == Short.class) {
                    val = (Short) m.invoke(object);
                } else if (field.getType() == float.class
                        || field.getType() == Float.class) {
                    val = (Float) m.invoke(object);
                } else if (field.getType() == BigDecimal.class) {
                    val = (BigDecimal) m.invoke(object);
                } else {
                    val = (Object) m.invoke(object);
                }
            } else {
                val = "";
            }
        } catch (Exception e) {
            log.error("ExportExcelUtils.getFieldValue", e);
        }
        return val;
    }

    private static String getMethodName(String fildeName) throws Exception {
        byte[] items = fildeName.getBytes();
        if((char) items[0] >= (char)'a'){
            items[0] = (byte) ((char) items[0] - 'a' + 'A');
        }
        return new String(items);
    }
}
